﻿#include "LoginScene.h"
#include "constant.h"
#include "Scene0910.h"
#pragma execution_character_set("utf-8")

Scene *LoginScene::createScene()
{
	return LoginScene::create();
}

bool LoginScene::init()
{
	if (!Scene::init())
	{
		return false;
	}
	//背景
	auto background = Sprite::create("login/Background.png");
	background->setPosition(VISIBLE_SIZE.width/2,VISIBLE_SIZE.height/2);
	addChild(background);

	//人物
	auto human = Sprite::create("login/denglu2.png");
	human->setPosition(VISIBLE_SIZE.width/5,VISIBLE_SIZE.height/2);
	human->setScale(0.5);
	addChild(human);

	//底部红条
	auto bottom = Sprite::create("login/denglu1.png");
	bottom->setPosition(VISIBLE_SIZE.width/2,bottom->getBoundingBox().size.height/2);
	addChild(bottom);

	//注册登录背景
	auto loginbackground = Sprite::create("login/denglu3.png");
	loginbackground->setPosition(VISIBLE_SIZE.width*0.7, VISIBLE_SIZE.height / 2.5);
	addChild(loginbackground);

	//返回按钮
	auto backBtn = Button::create("login/denglu8.png");
	backBtn->setScale(0.5);
	backBtn->setPosition(Vec2(
		(loginbackground->getPositionX()
			+ loginbackground->getBoundingBox().size.width / 2
			- backBtn->getBoundingBox().size.width / 2)*0.97,
			(loginbackground->getPositionY()
				+ loginbackground->getBoundingBox().size.height / 2
				- backBtn->getBoundingBox().size.height / 2)*0.95
	));
	addChild(backBtn);

	//注册、登录文本
	auto title = Label::createWithTTF("注册、登录","fonts/FZCY_GBK.ttf",42);
	title->setPosition(loginbackground->getPositionX(), backBtn->getPositionY());
	addChild(title);

	//♣
	auto plum_blossom = Sprite::create("login/denglu4.png");
	plum_blossom->setPosition(loginbackground->getPositionX()-loginbackground->getBoundingBox().size.width/2
		+plum_blossom->getBoundingBox().size.width+30,backBtn->getPositionY()-100);
	addChild(plum_blossom);

	//账号
	auto userNameLabel= Label::createWithTTF("账号", "fonts/FZCY_GBK.ttf", 35);
	userNameLabel->setPosition(plum_blossom->getPositionX()+plum_blossom->getBoundingBox().size.width/2
		+userNameLabel->getBoundingBox().size.width/2+20,plum_blossom->getPositionY());
	addChild(userNameLabel);

	//账号输入框
	userNameText = ui::EditBox::create(cocos2d::Size(330,40),ui::Scale9Sprite::create("login/denglu11.png"));
	userNameText->setPosition(Vec2(userNameLabel->getPositionX() +
		userNameLabel->getBoundingBox().size.width / 2 +
		userNameText->getBoundingBox().size.width / 2 + 20,
		userNameLabel->getPositionY()
	));
	userNameText->setPlaceHolder("请输入你的账号");
	userNameText->setPlaceholderFont("宋体",25);
	addChild(userNameText);

	//♠
	auto spade = Sprite::create("login/denglu5.png");
	spade->setPosition(plum_blossom->getPositionX(),plum_blossom->getPositionY()-spade->boundingBox().size.width/2-50);
	addChild(spade);

	//密码
	auto passwordLabel = Label::createWithTTF("密码", "fonts/FZCY_GBK.ttf", 35);
	passwordLabel->setPosition(userNameLabel->getPositionX(),spade->getPositionY());
	addChild(passwordLabel);

	//密码输入框
	 passwordText = ui::EditBox::create(cocos2d::Size(330, 40), ui::Scale9Sprite::create("login/denglu11.png"));
	passwordText->setPosition(Vec2(userNameText->getPositionX(), passwordLabel->getPositionY()));
	passwordText->setPlaceHolder("请输入你的密码");
	passwordText->setPlaceholderFont("宋体", 25);
	passwordText->setInputFlag(ui::EditBox::InputFlag::PASSWORD);
	addChild(passwordText);

	//记住账号
	auto rememberRadio = CheckBox::create("login/denglu10.png", "login/denglu9.png");
	rememberRadio->setPosition(Vec2(passwordLabel->getPositionX(),passwordLabel->getPositionY()- rememberRadio->getBoundingBox().size.height-40));
	addChild(rememberRadio);

	//记住账号文本
	auto rememberLabel = Label::createWithTTF("记住账号", "fonts/FZCY_GBK.ttf", 30);
	rememberLabel->setPosition(rememberRadio->getPositionX()+rememberLabel->getBoundingBox().size.width/2+30,rememberRadio->getPositionY());
	addChild(rememberLabel);

	//自动登录
	auto autoLoginRadio = CheckBox::create("login/denglu10.png", "login/denglu9.png");
	autoLoginRadio->setPosition(Vec2(loginbackground->getPositionX()+50,rememberLabel->getPositionY()));
	addChild(autoLoginRadio);

	//记住自动登录
	auto autoLoginLabel = Label::createWithTTF("自动登录", "fonts/FZCY_GBK.ttf", 30);
	autoLoginLabel->setPosition(autoLoginRadio->getPositionX() + autoLoginLabel->getBoundingBox().size.width / 2 + 30, autoLoginRadio->getPositionY());
	addChild(autoLoginLabel);

	//注册按钮
	auto regBtn = Button::create("login/denglu6.png");
	regBtn->setPosition(Vec2(loginbackground->getPositionX()-regBtn->getBoundingBox().size.width/2-50,autoLoginRadio->getPositionY()-60));
	addChild(regBtn);

	//登录按钮
	auto loginBtn = Button::create("login/denglu7.png");
	loginBtn->setPosition(Vec2(loginbackground->getPositionX() + loginBtn->getBoundingBox().size.width / 2 + 50, regBtn->getPositionY()));
	loginBtn->addTouchEventListener(CC_CALLBACK_2(LoginScene::login,this));
	addChild(loginBtn);

	//logo
	auto logo = Sprite::create("login/logo.png");
	logo->setPosition(loginbackground->getPositionX(),
		loginbackground->getPositionY()+loginbackground->getBoundingBox().size.height/2+logo->getBoundingBox().size.width/2-80);
	logo->setScale(0.6);
	addChild(logo);

	return true;
}

//点击登录到房间场景
void LoginScene::login(Ref * sender, Widget::TouchEventType type)
{
	if (type == Widget::TouchEventType::ENDED) {
		auto userName = userNameText->getText();
		auto password = passwordText->getText();
		log(userName);
		log(password);
		TransitionFade* transitionFade = TransitionFade::create(3, Scene0910::createScene());
		Director::getInstance()->replaceScene(transitionFade);
	}

}
